#include "TranslationMatrixInformation.h"



TranslationMatrixInformation::TranslationMatrixInformation()
{
    pTranslationMatrixData = pNormalMatrixData;
    ZeroTranslationMatrixData();
}

// 清零矩阵的平移部分// 第四行为平移部分
VOID TranslationMatrixInformation::ZeroTranslation()
{
    for (size_t col = 0; col < MATRIX_COL_COUNT; col++)
    {
        pTranslationMatrixData[3][col] = 0;
    }
}

// 清零矩阵内所有表示数据
VOID TranslationMatrixInformation::ZeroTranslationMatrixData()
{
    for (size_t RowIndex = 0; RowIndex < MATRIX_TRANSLATION_ROW_COUNT; RowIndex++)
    {
        for (size_t ColIndex = 0; ColIndex < MATRIX_TRANSLATION_COL_COUNT; ColIndex++)
        {
            pTranslationMatrixData[RowIndex][ColIndex] = 0;
        }
    }
    pTranslationMatrixData[3][3] = 1;
}

// 使用一个向量初始化矩阵平移部分
VOID TranslationMatrixInformation::SetTranslation(VectorInformation& vecInfo)
{
    pTranslationMatrixData[3][0] = vecInfo.GetX();
    pTranslationMatrixData[3][1] = vecInfo.GetY();
    pTranslationMatrixData[3][2] = vecInfo.GetZ();
}

// 获取平移部分
VectorInformation TranslationMatrixInformation::GetTranslation()
{
    return VectorInformation(pTranslationMatrixData[3][0], pTranslationMatrixData[3][1], pTranslationMatrixData[3][2]);
}

// 将原矩阵数据先转换成单位矩阵,并且使用一个向量初始化矩阵平移部分
VOID TranslationMatrixInformation::ConvertToIdentityMatrixSetTranslation(VectorInformation& vecInfo)
{
    ConvertToIdentityMatrix();
    SetTranslation(vecInfo);
}
// 打印平移矩阵
VOID TranslationMatrixInformation::ShowTranslationMatrixInformation()
{
    cout << "TranslationMatrixInformation Print:" << endl;
    for (size_t RowIndex = 0; RowIndex < MATRIX_TRANSLATION_ROW_COUNT; RowIndex++)
    {
        cout << "\t[";
        for (size_t ColIndex = 0; ColIndex < MATRIX_TRANSLATION_COL_COUNT; ColIndex++)
        {
            SetCoutWidth();
            double oValue = pTranslationMatrixData[RowIndex][ColIndex];
            cout << FormatOutputFloatValue(oValue);
            // 最后一列
            if (ColIndex != (MATRIX_TRANSLATION_COL_COUNT - 1))
            {
                cout << ", ";
            }
        }
        cout << "]" << endl;
    }
}
// 将原矩阵转换成单位矩阵
VOID TranslationMatrixInformation::ConvertToIdentityMatrix()
{
    MatrixInformation::ConvertToIdentityMatrix();
    ZeroTranslation();
}

VOID TranslationMatrixInformation::MatrixShear(ROTATE_TYPE ShearType, double s, double t)
{
    MatrixInformation::MatrixShear(ShearType, s, t);
    ZeroTranslation();
}

VOID TranslationMatrixInformation::MatrixImageXYZ(ROTATE_TYPE type, double k)
{
    MatrixInformation::MatrixImageXYZ(type);
    switch (type)
    {
    case ROTATE_TYPE::ROTATE_X: {
        DOUBLE row3[] = { 2.0f * k,0,0 };
        SetTranslationMatrixRowData(TranslationMatrixInformation::MATRIX_DATA_INDEX::INDEX3, row3);
        break;
    }
    case ROTATE_TYPE::ROTATE_Y: {
        DOUBLE row3[] = { 0, 2.0f * k,0 };
        SetTranslationMatrixRowData(TranslationMatrixInformation::MATRIX_DATA_INDEX::INDEX3, row3);
        break;
    }
    case ROTATE_TYPE::ROTATE_Z: {
        DOUBLE row3[] = { 0,0 ,2.0f * k };
        SetTranslationMatrixRowData(TranslationMatrixInformation::MATRIX_DATA_INDEX::INDEX3, row3);
        break;
    }
    default:
        break;
    }
}

VOID TranslationMatrixInformation::ArbitraryMatrixImage(VectorInformation& vectorInformation)
{
    MatrixInformation::ArbitraryMatrixImage(vectorInformation);
    ZeroTranslation();
}

VOID TranslationMatrixInformation::GetTranslationMatrixData(double data[MATRIX_TRANSLATION_ROW_COUNT][MATRIX_TRANSLATION_COL_COUNT])
{
    for (size_t RowIndex = 0; RowIndex < MATRIX_TRANSLATION_ROW_COUNT; RowIndex++)
    {
        for (size_t ColIndex = 0; ColIndex < MATRIX_TRANSLATION_COL_COUNT; ColIndex++)
        {
            data[RowIndex][ColIndex] = pNormalMatrixData[RowIndex][ColIndex];
        }
    }
}

VOID TranslationMatrixInformation::SetTranslationMatrixData(double data[MATRIX_TRANSLATION_ROW_COUNT][MATRIX_TRANSLATION_COL_COUNT])
{
    for (size_t RowIndex = 0; RowIndex < MATRIX_TRANSLATION_ROW_COUNT; RowIndex++)
    {
        for (size_t ColIndex = 0; ColIndex < MATRIX_TRANSLATION_COL_COUNT; ColIndex++)
        {
            pNormalMatrixData[RowIndex][ColIndex] = data[RowIndex][ColIndex];
        }
    }
}

VOID TranslationMatrixInformation::SetTranslationMatrixData(double data[MATRIX_ROW_COUNT][MATRIX_COL_COUNT])
{
    MatrixInformation::SetMatrixData(data);
}

VOID TranslationMatrixInformation::ProjectionMatrix(VectorInformation& vectorInformation)
{
    MatrixInformation::ProjectionMatrix(vectorInformation);
    ZeroTranslation();
}

VOID TranslationMatrixInformation::ProjectionMatrix(double x, double y, double z)
{
    VectorInformation vec(x, y, z);
    ProjectionMatrix(vec);
}

VOID TranslationMatrixInformation::SaclingMatrix(double x, double y, double z)
{
    MatrixInformation::SaclingMatrix(x, y, z);
    ZeroTranslation();
}

VOID TranslationMatrixInformation::SaclingMatrix(VectorInformation& vectorInformation)
{
    MatrixInformation::SaclingMatrix(vectorInformation);
    ZeroTranslation();
}

VOID TranslationMatrixInformation::SetTranslationMatrixRowData(MATRIX_DATA_INDEX rowIndex, double rowData[3])
{
    switch (rowIndex)
    {
    case TranslationMatrixInformation::MATRIX_DATA_INDEX::INDEX0:
    case TranslationMatrixInformation::MATRIX_DATA_INDEX::INDEX1:
    case TranslationMatrixInformation::MATRIX_DATA_INDEX::INDEX2:
        MatrixInformation::SetMatrixRowData(static_cast<MatrixInformation::MATRIX_DATA_INDEX>(rowIndex), rowData);
        break;
    case TranslationMatrixInformation::MATRIX_DATA_INDEX::INDEX3:
        for (size_t col = 0; col < 3; col++)
        {
            UINT row = static_cast<UINT>(rowIndex);
            pNormalMatrixData[row][col] = rowData[col];
        }
        break;
    default:
        break;
    }
}

VOID TranslationMatrixInformation::RotationMatrix(ROTATE_TYPE type, double angle)
{
    MatrixInformation::RotationMatrix(type, angle);
    ZeroTranslation();
}

TranslationMatrixInformation operator*(TranslationMatrixInformation& trMatrixInfo1, TranslationMatrixInformation& trMatrixInfo2)
{
    MatrixInformation matrix =    MatrixMultiply(trMatrixInfo1, trMatrixInfo2);
    double data[MATRIX_ROW_COUNT][MATRIX_COL_COUNT];
    matrix.GetMatrixData(data);
    TranslationMatrixInformation translationMatrix;
    translationMatrix.SetMatrixData(data);


    double data1[MATRIX_TRANSLATION_ROW_COUNT][MATRIX_TRANSLATION_COL_COUNT];
    double data2[MATRIX_TRANSLATION_ROW_COUNT][MATRIX_TRANSLATION_COL_COUNT];
    trMatrixInfo1.GetTranslationMatrixData(data1);
    trMatrixInfo2.GetTranslationMatrixData(data2);

    double m31 = data1[3][0];
    double m33 = data1[3][2];

    double tx = m31 * data2[0][0] + data1[3][1] * data2[1][0] + m33 * data2[2][0] + data2[3][0];
    double ty = m31 * data2[0][1] + data1[3][1] * data2[1][1] + m33 * data2[2][1] + data2[3][1];
    double tz = m31 * data2[0][2] + data1[3][2] * data2[1][2] + m33 * data2[2][2] + data2[3][2];
    double row3[] = { tx,ty,tz };
    translationMatrix.SetTranslationMatrixRowData(TranslationMatrixInformation::MATRIX_DATA_INDEX::INDEX3, row3);

    return translationMatrix;
}
